package com.smartx.app.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.smartx.Network;
import com.smartx.app.R;
import com.smartx.app.SatApp;
import com.smartx.app.fragment.ReceiveFragment;
import com.smartx.app.fragment.SendFragment;
import com.smartx.app.fragment.SettingsFragment;
import com.smartx.app.fragment.TransactionFragment;
import com.smartx.app.model.NodeInfoModel;
import com.smartx.app.util.DefaultHttpRequest;
import com.smartx.app.util.HttpRequestManager;
import com.smartx.config.Constants;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG="wallet";
    private static int REQUEST_PERMISSION_CODE = 1;

    String[] permissions = new String[]{
            Manifest.permission.CAMERA,
            Manifest.permission.INTERNET,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CALL_PHONE
    };
    // 声明一个集合，在后面的代码中用来存储用户拒绝授权的权
    List<String> mPermissionList = new ArrayList<>();

    private ViewPager mViewPager;
    private MyFragmentAdapter adapter;
    private FragmentManager mFragmentManager;
    private TransactionFragment mTransactionFragment;
    private ReceiveFragment mReceiveFragment;
    private SendFragment mSendFragment;
    private SettingsFragment mSettingsFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SatApp.getInstance().setContext(this);

        //TODO:获取flash权限失败，这里还需要自己调整一下
        initPermission();
        initNetwork();
        initWallet();
        initView();
    }

    private void initPermission() {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, permissions, REQUEST_PERMISSION_CODE);
            }

            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, permissions, REQUEST_PERMISSION_CODE);
            }

            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, permissions, REQUEST_PERMISSION_CODE);
            }

        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_PERMISSION_CODE) {
            for (int i = 0; i < permissions.length; i++) {
                Log.i("MainActivity", "申请的权限为：" + permissions[i] + ",申请结果：" + grantResults[i]);
            }
        }
    }

    private void initView(){
        mViewPager = findViewById(R.id.view_pager);
        mFragmentManager = getSupportFragmentManager();
        mTransactionFragment = new TransactionFragment();
        mSendFragment = new SendFragment();
        mReceiveFragment = new ReceiveFragment();
        mSettingsFragment = new SettingsFragment();

        findViewById(R.id.main_page_tab_menu1_layout).setOnClickListener(this);
        findViewById(R.id.main_page_tab_menu2_layout).setOnClickListener(this);
        findViewById(R.id.main_page_tab_menu3_layout).setOnClickListener(this);
        findViewById(R.id.main_page_tab_menu4_layout).setOnClickListener(this);
        adapter = new MyFragmentAdapter(mFragmentManager);
        mViewPager.setAdapter(adapter);
        mViewPager.setCurrentItem(0);
    }

    private void initNetwork(){
        //获取远端网络类型以及节点信息
        DefaultHttpRequest request = new DefaultHttpRequest() {
            @Override
            public void onSuccess(String json) {
                NodeInfoModel nodeInfo = NodeInfoModel.getNodeInfo(json);
                if(nodeInfo != null){
                    String network = nodeInfo.network;
                    SatApp.getInstance().setNetwork(Network.of(network));
                }
            }

            @Override
            public String url() {
                return Constants.URL_GET_NODE_INFO;
            }
        };

        HttpRequestManager.manager().get(request);
    }

    private void initWallet(){
        //如果钱包文件不存在
//        if(!Tools.isWalletFileExist()){
//            Intent intent = new Intent(this,SplashActivity.class);
//            startActivityForResult(intent,Constants.REQUEST_CODE_CREATE_WALLET);
//            return;
//        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == Constants.REQUEST_CODE_CREATE_WALLET){

        }else{
            Log.w(TAG,"illegal request code received");
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.main_page_tab_menu1_layout:
                mViewPager.setCurrentItem(0);
                break;
            case R.id.main_page_tab_menu2_layout:
                mViewPager.setCurrentItem(1);
                break;
            case R.id.main_page_tab_menu3_layout:
                mViewPager.setCurrentItem(2);
                break;
            case R.id.main_page_tab_menu4_layout:
                mViewPager.setCurrentItem(3);
                break;
        }
    }

    @Override
    protected void onResume(){
        super.onResume();
    }

    public class MyFragmentAdapter extends FragmentPagerAdapter {

        public MyFragmentAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = null;
            switch (position) {
                case 0:
                    if (null == mTransactionFragment) {
                        mTransactionFragment = new TransactionFragment();
                    }
                    fragment = mTransactionFragment;
                    break;
                case 1:
                    if (mSendFragment == null) {
                        mSendFragment = new SendFragment();
                    }
                    fragment = mSendFragment;
                    break;
                case 2:
                    if (mReceiveFragment == null) {
                        mReceiveFragment = new ReceiveFragment();
                    }
                    fragment = mReceiveFragment;
                    break;
                case 3:
                    if (null == mSettingsFragment) {
                        mSettingsFragment = new SettingsFragment();
                    }
                    fragment = mSettingsFragment;
                    break;
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return 4;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            super.destroyItem(container, position, object);
        }
    }
}
